$(document).ready(function() {
    var currentAudio;
    $('.poem-wrap').each(function(i, e) {
        initAudio(e);
    });
    
    $('#readLink').click(function(){
        window.bridge.jsConfirmBtn();
    },false)
    function initAudio(bindElement) {
        var _audio;
        var $be = $(bindElement);
        if (window['Audio'] && (_audio = new Audio()).canPlayType('audio/mpeg')) {
            $be.find('.play-button').click(function() {
                //pause previous play
                if (currentAudio && currentAudio != _audio) {
                    currentAudio.pause();
                    currentAudio.currentTime = 0;
                }

                if (_audio.paused) {
                    currentAudio = _audio;
                    _audio.play();

                } else {
                    _audio.pause();
                }
            }, false);
            _audio.addEventListener('loadedmetadata', function() {
                duration = this.duration;
                _audio.currentTime = 0;
            }, false);
            _audio.addEventListener('play', function() {
                $be.find('.play-button img').attr('src', 'images/pause.png');
                $be.find('.poem-content').slideDown(100);
                $be.find('.play-control').css('background-color', '#f5f5f5');
            }, false);
            _audio.addEventListener('pause', function() {
                $be.find('.play-button img').attr('src', 'images/play.png');
                $be.find('.poem-content').slideUp(100);
                $be.find('.play-control').css('background-color', '#fff');
            }, false);
            _audio.addEventListener('ended', function() {
                this.pause();
                this.currentTime = 0;
            }, false);
            _audio.addEventListener('error', function() {
                console.log('ERROR');
            }, false);
            _audio.addEventListener('timeupdate', function() {
                if(_audio&&_audio.duration!=0){
                    var percentage = Number(this.currentTime / _audio.duration * 100).toFixed(2) + '%';

                    $be.find('.poem-progress').css('width', percentage);
                }
                
            }, false);
            _audio.src = $be.data('src');
            _audio.load();
            bindElement.audio = _audio;
            //设置src
            return _audio;
        }
        return;
    }
        
});